package com.example.starseas_phone;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.starseas_phone.Adapters.Tea_AllStudentslistviewAdapter;
import com.example.starseas_phone.Adapters.WorkerManagelistviewAdapter;
import com.example.starseas_phone.Entity.Students;
import com.example.starseas_phone.Entity.Workers;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class WorkerManageActivity extends AppCompatActivity {

    private ListView worker_manage_list;
    private WorkerManagelistviewAdapter adapter;
    private List<Workers> list = new ArrayList<>();

    private FloatingActionButton worker_manage_floatbtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_worker_manage);
        initid();
        initview();
        worker_manage_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Workers workers = adapter.getItem(i);
                Intent intent = new Intent(WorkerManageActivity.this,Worker_detailsActivity.class);
                intent.putExtra("workers",workers);
                startActivity(intent);
            }
        });
        worker_manage_floatbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(WorkerManageActivity.this,AddWorkerActivity.class);
                startActivity(intent);
            }
        });
    }

    private void initid(){
        worker_manage_list = (ListView) findViewById(R.id.worker_manage_list);
        adapter = new WorkerManagelistviewAdapter(WorkerManageActivity.this,R.layout.workermanage_listview,list);
        worker_manage_floatbtn = (FloatingActionButton) findViewById(R.id.worker_manage_floatbtn);
    }

    private void initview(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    Request request = new Request.Builder()
                            .url(Sto.NetIP+"/api/Worker/GetAllWorker")
                            .get()
                            .build();
                    Response response = client.newCall(request).execute();
                    String responseString = response.body().string();
                    JSONObject jsonObjects = new JSONObject(responseString);
                    String msg = jsonObjects.getString("message");
                    JSONArray jsonArray = new JSONArray(msg);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        int Id = jsonObject.getInt("Id");
                        String WorkerId = jsonObject.getString("WorkerId");
                        int WorkerAge = jsonObject.getInt("WorkerAge");
                        String WorkerName = jsonObject.getString("WorkerName");
                        String WorkerbackTime = jsonObject.getString("WorkerbackTime");
                        String WorkerCarid = jsonObject.getString("WorkerCarid");
                        String WorkerEmail = jsonObject.getString("WorkerEmail");
                        String WorkerLocal = jsonObject.getString("WorkerLocal");
                        String WorkerPhone = jsonObject.getString("WorkerPhone");
                        String WorkerSex = jsonObject.getString("WorkerSex");
                        int PositionId = jsonObject.getInt("PositionId");
                        Workers workers = new Workers(Id,WorkerId,WorkerName,WorkerAge,WorkerSex,WorkerCarid,WorkerPhone,PositionId,WorkerLocal,WorkerbackTime,WorkerEmail);
                        list.add(workers);
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            adapter.notifyDataSetChanged();
                            worker_manage_list.setAdapter(adapter);
                        }
                    });
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

    @Override
    protected void onRestart() {
        list.clear();
        initview();
        super.onRestart();
    }
}